package com.ego.e

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.ego.e.adapter.EAdapter
import com.ego.e.adapter.StringAdapter
import com.ego.e.dialog.ELoading
import com.ego.e.entity.Links
import com.ego.e.entity.Person
import com.ego.e.entity.Singleton
import com.ego.e.utils.EGson
import com.ego.e.utils.EUtils
import kotlinx.android.synthetic.main.activity_kotlin.*

class KotlinActivity : AppCompatActivity() {

    var p = Person("Hello")
    val k = EKotlin()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)

        tv_hello.text = k.kotlin()

        println(p.hello())

        println(k.sum(2,3))

        println(k.isEmpty(null))

        var list:ArrayList<String> = arrayListOf("Hello","Kotlin")
        k.append(list)

        k.inRange(2)

        k.format("hello")

        k.ext()

        k.switch(3)

        k.label()

        EUtils.vertical(rv_list)
        var adapter = StringAdapter(this,list())
        rv_list.adapter = adapter

        adapter.listener = object : EAdapter.EListener<String>{
            override fun onClick(target: View, data: String, position: Int) {
                toast(data)
            }
        }

        println(p.skill)

        //将一个对象 解构(destructure) 为多个变量
        var (value) = p
        println(value)
        println(Singleton.kotlin())

        val data = "{\"name\": \"Google\",\"url\": \"http://www.google.com\"}"
        val array = "[{\"name\": \"Google\",\"url\": \"http://www.google.com\"},{\"name\": \"Android\",\"url\": \"http://www.google.com\"}]"
//        var link = EGson.data(data,Links::class.java)
        var link = EGson.data<Links>(data)
        val links = EGson.mutable<Links>(array)

        println(link)

        //println(links)
        for (l in links){
            println(l.name)
        }


        ELoading(this).loading()
    }

    fun toast(toast:String){
        Toast.makeText(this,toast,Toast.LENGTH_LONG).show()
    }


    /**扩展函数：函数ext成为EKotlin类的函数*/
    fun EKotlin.ext(){
        println("扩展函数：函数ext成为EKotlin类的函数")

        var json = ""
        val pl = EGson.data(json,Person::class.java)
    }


    /**"Hello","Kotlin","Adapter","NBA","CBA","Basketball","Android"*/
    fun list():MutableList<String>{
        val list:MutableList<String> = mutableListOf()
        list.add("Hello")
        list.add("Kotlin")
        list.add("Adapter")
        list.add("NBA")
        list.add("CBA")
        list.add("Basketball")
        list.add("Android")
        list.add("lxy")
        return list
    }

}
